package br.com.locautowebbd.locatario;

import java.sql.*;

import br.com.locautowebbd.util.JDBCConnectionUtil;
import br.com.locautowebbd.util.RepositorioException;

public class RepositorioLocatariosJDBC implements RepositorioLocatarios {

	public static final int PESSOAFISICA = 1;
	  public static final int AGENCIA = 2;
	public void atualizar(Locatario lt) throws LocatarioInexistenteException,
			RepositorioException {
		Statement stmt = null;
		try {
			String query = "UPDATE TB_LOCATARIO set " + " login='" + lt.getLogin()
					+ "' " + "WHERE codigo='" + lt.getLogin() + "'";
			stmt = JDBCConnectionUtil.obterConexao()
					.createStatement();
			int i = stmt.executeUpdate(query);
			if (i == 0) {
				throw new LocatarioInexistenteException(000);
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
			throw new RepositorioException(ex);
		} finally {
			try {
				stmt.close();
			} catch (SQLException ex) {
				ex.printStackTrace();
				throw new RepositorioException(ex);
			}
		}
	}

	public void inserir(Locatario lt) throws RepositorioException{
		Statement stmt = null;
		try {
			String query = "INSERT INTO TB_LOCATARIO VALUES( " + "'" + lt.getCodigo()
					+ "', " + "'" + lt.getLogin() + "'," + lt.getSenha() + ")"; 
			stmt = JDBCConnectionUtil.obterConexao().createStatement();
			stmt.executeUpdate(query);
		} catch (SQLException ex) {
			ex.printStackTrace();
			throw new RepositorioException(ex);
		} finally {
			try {
				stmt.close();
			} catch (SQLException ex) {
				ex.printStackTrace();
				throw new RepositorioException(ex);
			}
		}
	}

	public Locatario procurar(int codigo) throws LocatarioInexistenteException,
			RepositorioException {
		Statement stmt = null;
		ResultSet rs = null;
		Locatario procurado = null;
		try {
			String query = "SELECT * FROM TB_LOCATARIO " + "WHERE codigo='" + codigo+ "'";
			stmt = JDBCConnectionUtil.obterConexao()
					.createStatement();
			rs = stmt.executeQuery(query);
			if (rs.next()) {

				codigo = rs.getInt(codigo);
				String login = rs.getString("login");
				String senha = rs.getString("senha");
				String email = rs.getString("email");

				procurado = new Locatario(codigo,login, senha, email);

			} else {
				throw new LocatarioInexistenteException(codigo);
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
			throw new RepositorioException(ex);
		} finally {
			try {
				rs.close();
				stmt.close();
			} catch (SQLException ex) {
				ex.printStackTrace();
				throw new RepositorioException(ex);
			}
		}

		return procurado;

	}

	public void remover(int codigo) throws LocatarioInexistenteException,
			RepositorioException {
		Statement stmt = null;
		try {
			String query = "DELETE FROM LOCATARIO " + "WHERE codigo='" + codigo + "'";
			stmt = JDBCConnectionUtil.obterConexao()
					.createStatement();
			int i = stmt.executeUpdate(query);
			if (i == 0) {
				throw new LocatarioInexistenteException(000);
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
			throw new RepositorioException(ex);
		} finally {
			try {
				stmt.close();
			} catch (SQLException ex) {
				ex.printStackTrace();
				throw new RepositorioException(ex);
			}
		}
	}

	public boolean existe(int codigo) throws RepositorioException {
		Statement stmt = null;
		ResultSet rs = null;
		boolean resp = false;
		try {
			String query = "SELECT * FROM LOCATARIO " + "WHERE codigo='" + codigo + "'";
			stmt = JDBCConnectionUtil.obterConexao()
					.createStatement();
			rs = stmt.executeQuery(query);
			if (rs.next()) {
				resp = true;
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
			throw new RepositorioException(ex);
		} finally {
			try {
				rs.close();
				stmt.close();
			} catch (SQLException ex) {
				ex.printStackTrace();
				throw new RepositorioException(ex);
			}
		}

		return resp;
	}
}
